﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MetadataBasedConfigArch.Web.Admin
{
    public partial class ManageDocuments: System.Web.UI.Page
    {
        #region Page Load
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #endregion

        #region Properties

        protected PageState PageState
        {
            get
            {
                return (PageState)(ViewState["Mode"] ?? PageState.Show);
            }
            set
            {
                ViewState["Mode"] = value;
            }
        }

        protected Int64 DocumentID
        {
            get
            {
                return Convert.ToInt64(ViewState["DocumentID"] ?? 0);
            }
            set
            {
                ViewState["DocumentID"] = value;
            }
        }
        
        #endregion

        #region Bind Data

        protected void BindData()
        {
            using (Model.MDAEntities entities = EntityUtil.LoadEntities())
            {
                var allDocuments = (from d in entities.Documents
                                    where d.TenantID == Util.TenantID
                                    select d).ToList();
                MainGridView.DataSource = allDocuments;
                MainGridView.DataBind();
            } 
            DocumentID = 0;
        }

        #endregion

        #region Events

        protected void AddLinkButton_Click(object sender, EventArgs e)
        {
            if (PageState == Web.PageState.Show)
            {
                EditPanel.Visible = true;
                ShowPanel.Visible = false;
                PageState = Web.PageState.New;
                AddLinkButton.Text = "Go Back";
            }
            else
            {
                EditPanel.Visible = false;
                ShowPanel.Visible = true;
                PageState = Web.PageState.Show;
                AddLinkButton.Text = "Add Document";
            } 
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            string fileFullPath = string.Empty ;
            if (DocumentFileUpload.HasFile)
            {
                string filePath = "~/Upload/" + Guid.NewGuid().ToString("N");
                int index = DocumentFileUpload.FileName.LastIndexOf(".");
                string fileExtension = string.Empty;
                if (index > 0)
                {
                    fileExtension = DocumentFileUpload.FileName.Substring(index);
                }
                string absoluteFilePath = Server.MapPath(filePath + fileExtension);
                DocumentFileUpload.SaveAs(absoluteFilePath);
                fileFullPath = filePath +fileExtension ;
            }
            if (PageState == Web.PageState.New)
            {
                if(!String.IsNullOrEmpty (fileFullPath ))
                {
                    using (Model.MDAEntities entities = EntityUtil.LoadEntities())
                    {
                        var document = new Model.Document();
                        document.Name = NameTextBox.Text.Trim();
                        document.Path = fileFullPath;
                        document.TenantID = Util.TenantID;
                        entities.AddToDocuments(document);
                        entities.SaveChanges();
                    }
                }
            }
            else if(PageState == Web.PageState.Edit)
            {
                if (DocumentID > 0)
                {
                    using (Model.MDAEntities entities = EntityUtil.LoadEntities())
                    {
                        var document = (from d in entities.Documents where d.ID == DocumentID select d).FirstOrDefault();
                        if (document != null)
                        {
                            document.Name = NameTextBox.Text.Trim();
                            if (!String.IsNullOrEmpty(fileFullPath))
                            {
                                document.Path = fileFullPath;
                            }
                            entities.SaveChanges();
                        }
                    }
                }
            }
            EditPanel.Visible = false;
            ShowPanel.Visible = true;
            PageState = Web.PageState.Show;
            AddLinkButton.Text = "Add Document";
            BindData();
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            EditPanel.Visible = false;
            ShowPanel.Visible = true;
            PageState = Web.PageState.Show;
            AddLinkButton.Text = "Add Document";
            BindData();
        }

        #endregion

        #region Grid View Events
         
        protected void MainGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var document = e.Row .DataItem as Model.Document;
                HyperLink openDocumentLink = e.Row.Cells[1].FindControl("DocumentHyperLink") as HyperLink;
                openDocumentLink.NavigateUrl = document.Path;

                Button editButton = e.Row.Cells[2].FindControl("EditButton") as Button;
                editButton.CommandArgument = document.ID.ToString();

                Button deleteButton = e.Row.Cells[2].FindControl("DeleteButton") as Button;
                editButton.CommandArgument = document.ID.ToString();

            }
        }

        protected void MainGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandArgument!=null)
            {
                var documentId = Convert.ToInt64(e.CommandArgument??"0");
                if (documentId > 0)
                {
                    switch (e.CommandName)
                    {
                        case "EditD":
                            {
                                using (Model.MDAEntities entities = EntityUtil.LoadEntities())
                                {
                                    var document = (from d in entities.Documents where d.ID == documentId && d.TenantID == Util.TenantID select d).FirstOrDefault();
                                    if (document != null)
                                    {
                                        NameTextBox.Text = document.Name; 
                                    }
                                }
                                EditPanel.Visible = true;
                                ShowPanel.Visible = false;
                                PageState = Web.PageState.Edit;
                                DocumentID = documentId; 
                                AddLinkButton.Text = "Go Back";
                                break;
                            }
                        case "Delete":
                            {
                                using (Model.MDAEntities entities = EntityUtil.LoadEntities())
                                {
                                    var document = (from d in entities.Documents where d.ID == documentId && d.TenantID == Util.TenantID select d).FirstOrDefault();
                                    if (document != null)
                                    {
                                        entities.DeleteObject(document);
                                        entities.SaveChanges();
                                    }
                                }
                                BindData();
                                break;
                            }
                        default:
                            {
                                break;
                            }
                    }
                }
            }
        }

        #endregion


    }
}